<template>
  <div>
    <!-- 顶部搜索 -->
    <div class="search-row search-box">
      <el-form :inline="true" :model="searchParams" class="demo-form-inline">
        <el-form-item label="人审时间">
          <el-date-picker
            v-model="timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="initDate"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="success" icon="el-icon-search" @click="handleSearch"
            >查询</el-button
          >
        </el-form-item>

        <el-form-item>
          <el-button type="success" plain @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--table 刷新-->
    <div class="table-conBtn">
      <freshCom @searchList="searchList"></freshCom>
    </div>

    <!--table 列表-->
    <div class="table-box">
      <el-table
        ref="table"
        :data="tableData"
        v-loading="loading"
        :fit="true"
        max-height="490"
        stripe
      >
        <el-table-column type="index" label="序号">
          <template slot-scope="scope">
            {{
              scope.$index
                | filterPage(pagingParams.pageNum, pagingParams.pageSize)
            }}
          </template>
        </el-table-column>

        <el-table-column label="类型" prop="workType" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.workType | jobTypeFilter }}</span>
          </template>
        </el-table-column>

        <el-table-column label="配文" prop="content" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.content" v-html="filterContent(scope.row)">
              {{ scope.row.content }}
            </span>
            <span v-else> --- </span>
          </template>
        </el-table-column>

        <el-table-column label="视图" prop="imgInfo" align="center">
          <template slot-scope="scope">
            <span v-if="!scope.row.video">
              <listImgShow :imgInfo="scope.row.imgInfo"></listImgShow>
            </span>

            <span v-else class="img-box" @click="videoPlay(scope.row.video)">
              <div class="img-inner-box">
                <el-image fit="contain" :src="scope.row.imgInfo"></el-image>
              </div>
            </span>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" prop="submit" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.submit | timeToTime }}</span>
          </template>
        </el-table-column>

        <el-table-column label="未通过原因" prop="reason" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.reason">{{ scope.row.reason }}</span>
            <span v-else> --- </span>
          </template>
        </el-table-column>

        <el-table-column label="人审时间" prop="checkTime" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.checkTime">{{
              scope.row.checkTime | timeToTime
            }}</span>
            <span v-else> --- </span>
          </template>
        </el-table-column>

        <el-table-column label="审核人" prop="checkName" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.checkName">{{ scope.row.checkName }}</span>
            <span v-else> --- </span>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 视频组件 -->
    <showVideo v-if="videoData.isShow" :video-data="videoData"></showVideo>

    <!--分页-全局组件-->
    <pageCom
      :searchParams="pagingParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
  </div>
</template>

<script>
import showVideo from '@common/showVideo'
import { getVideoImg } from '@utils/getFaceImg'
import { filterCon, getVideoPara } from '@utils/pubFunction'
import { getWorksList } from '@/api/commentCheck/unpassList'

const jobType = {
  TEXT: { text: '图文' },
  VIDEO: { text: '短视频' }
}

export default {
  msg: '作品帖子',

  components: {
    showVideo
  },

  data() {
    return {
      total: 0,
      loading: true,

      tableData: [],
      tableTemp: [],
      tempSize: 0,

      timeRange: [],

      pagingParams: {
        pageNum: 1,
        pageSize: 10
      },

      searchParams: {
        timeType: 'APPROVEDTIME', // 时间类型
        startTime: '',
        endTime: '',
        checkStatus: 'UNPASS', // 人审状态
        sortRule: 'APPROVED_TIME_DESC', // 人审时间倒序
        userType: 'DAPENGACCOUNT', // 大鹏号
        userValue: '' // 大鹏号
      },

      videoData: {
        isShow: false,
        vid: ''
      },

      polivNum: {
        polyvUserId: '',
        polyvSecretKey: ''
      }
    }
  },

  computed: {
    dpAccount() {
      return this.$route.query.dpAccount
    }
  },

  filters: {
    jobTypeFilter(type) {
      return jobType[type].text
    }
  },

  created() {
    if (this.dpAccount) {
      this.searchParams.userValue = this.dpAccount
    }

    this.searchList()
  },

  methods: {
    initDate(val){
      if(!val){
        this.searchParams.startTime = ''
        this.searchParams.endTime = ''  
      }else{
        this.searchParams.startTime = val[0]
        this.searchParams.endTime = val[1]
      }
    },
    searchList() {
      this.loading = true

      if (this.timeRange) {
        this.searchParams.startTime = this.timeRange[0]
        this.searchParams.endTime = this.timeRange[1]
      }

      let params = { ...this.searchParams, ...this.pagingParams }
      

      getWorksList(
        params,
        (res) => {
          this.loading = false
          this.tableTemp = res.rows
          this.tableData = res.rows
          this.tempSize = res.rows.length
          this.total = res.total
          this.getPolivNum()
        },
        (err) => {
          this.$message({
            message: err.message,
            type: 'warning'
          })
        }
      )
    },

    // 播放视频
    videoPlay(vid) {
      if (!vid) {
        this.$message.warning('无视频资源！')
        return
      }

      this.videoData.vid = vid
      this.videoData.isShow = true
    },

    // 获取系统配置-保利威视
    getPolivNum() {
      let _this = this

      getVideoPara().then(
        (res) => {
          this.polivNum.polyvUserId = res.polyvUserId
          this.polivNum.polyvSecretKey = res.polyvSecretKey

          let n = 0

          if (!this.tableTemp || this.tableTemp.length === 0) {
            return
          }

          this.tableTemp.forEach((item, index) => {
            _this.getImg(item.video, index).then(
              (data) => {
                n++
                _this.setData(n)
              },
              () => {
                n++
                _this.setData(n)
              }
            )
          })
        },
        (res) => {
          _this.tableData = [..._this.tableTemp]
        }
      )
    },

    // 符合条件后, 给table赋值
    setData(n) {
      if (n === this.tempSize) {
        this.loading = false
        this.tableData = [...this.tableTemp]
      }
    },

    // 获取封面图
    getImg(video, index) {
      if (!video) return Promise.resolve(false)
      let _this = this
      return getVideoImg(video, this.polivNum).then(
        (res) => {
          _this.tableTemp[index].imgInfo = res
          return Promise.resolve(true)
        },
        (error) => {
          return Promise.reject(error)
        }
      )
    },

    // 过滤返回评论内容
    filterContent(item) {
      return filterCon(item)
    },

    // 查询
    handleSearch() {
      this.pagingParams.pageNum = 1
      this.searchList()
    },

    // 重置
    handleReset() {
      this.timeRange = []
      this.searchParams.startTime = ''
      this.searchParams.endTime = ''
    }
  }
}
</script>
